/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.tree;

import java.awt.*;
import javax.swing.*;

import jdbcmanager.app.*;
import jdbcmanager.swing.*;
import jdbcmanager.diagram.*;
import jdbcmanager.dbtools.*;
import jdbcmanager.dbtools.agentwise.*;
import jdbcmanager.dbtools.datatypes.*;

/**
 * Tree nodes representing a diagram DB entity.
 *
 * @author Francisco Morero Peyrona
 */
final class NodeDiagram extends JdbcTreeNode
{
   private JFrameMain.JDBCManagerInternalFrame frame = null;
   
   /**
    * Called to create the "root" of this branch  (allows children)
    * 
    * @param sCaption
    */
   NodeDiagram( String sCatalog, String sSchema )
   {
      super( icon_tree_diagram, new DbEntityDescriptor( sCatalog, sSchema, "Diagrams" ) );
      
      // Trick to force the tree to show the RootHandles (it will be removed before adding real nodes)
      add( new NodeCatalog( "" ) );
      setInvalidActions();
   }

   /**
    * Called to create each leaf of this branch (do NOT allows children)
    *
    * @param sCaption
    * @param sCatalogName
    */
   NodeDiagram( DbEntityDescriptor ded, boolean bDiagram )
   {
      super( icon_tree_diagram, ded );

      setAllowsChildren( ! bDiagram );
      setInvalidActions();
   }
   
   boolean isInitialised()
   {
      return this.bWasInit || ! getAllowsChildren();
   }

   boolean new_()
   {
      boolean bSuccess = false;
      
      try
      {
         DbEntityDescriptor ded = getNewDiagramInfo();
         
         if( ded != null )
         {
            (new DbDiagram( this.ded )).new_( ded );
            _open( ded.getName() );
            bSuccess = true;
         }
      }
      catch( Exception exc )
      {
         App.showException( exc );
      }
      
      return bSuccess;
   }

   void open()
   {
      if( this.frame == null || this.frame.isClosed() )
      {
         App.setCursor( Cursor.WAIT_CURSOR );
         _open( getName() );
         App.restoreCursor();
      }
      else
      {
         this.frame.setSelected( true );
         this.frame.restore();
      }
   }
   
   boolean edit()
   {
      String sDiagram = App.getStringUsingDialog( "Rename diagram", "Enter new diagram name", getName(), false );

      if( sDiagram != null )
         rename( sDiagram );
      
      return sDiagram != null;
   }
   
   boolean rename( String sNewName )
   {
      boolean   bSuccess = false;
      DbDiagram diagram  = new DbDiagram( this.ded );
      
      if( diagram.rename( sNewName ) )
      {
         bSuccess = true;
         
         if( this.frame != null )
            this.frame.rename( sNewName );
      }
      
      return bSuccess;
   }

   boolean delete()
   { 
      boolean   bSuccess = false;
      DbDiagram diagram  = new DbDiagram( this.ded );
      
      if( diagram.delete() )
      {
         bSuccess = true;
         
         if( this.frame != null )
            this.frame.dispose();
      }
      
      return bSuccess;
   }

   void info()
   {
      // Nothing to do
   }
   
   //-------------------------------------------------------------------------//
   
   private void _open( String sName )
   {
      App.setCursor( Cursor.WAIT_CURSOR );

      this.frame = App.getFrameMain().openDiagram( new DbEntityDescriptor( getCatalog(), getSchema(), sName ) );
      
      App.restoreCursor();
   }
   
   //------------------------------------------------------------------------//
   
   private DbEntityDescriptor getNewDiagramInfo()
   {
      DbEntityDescriptor ded = null;
      AgentMetaData      meta = DbUtil.getAgentDBMS().getAgentMetaData();
      
      JTextField txt = new JTextField();
      JLabel     lbl = new JLabel( "Select "+ meta.getSchemaTerm() );
      JComboBox  cmb = new JComboBox( meta.getSchemas() );
      
      JPanel     pnl = new JPanel( new GridLayout( 2,2,0,12 ) );
                 pnl.add( new JLabel( "Diagram name") );
                 pnl.add( txt );
                 pnl.add( lbl );
                 pnl.add( cmb );
                 
      if( cmb.getItemCount() == 0 )
      {
         lbl.setVisible( false );
         cmb.setVisible( false );
      }
      else
      {
         cmb.setSelectedIndex( 0 );
      }

      JxDialog dlg = new JxDialog( "Create new diagram" );
               dlg.getContentPane().add( pnl );
               dlg.pack();
               dlg.center();
               dlg.setVisible( true );
      
      if( dlg.getCloseOption() == JxDialog.ACCEPTED )
      {
         if( txt.getText().trim().length() > 0 )
         {
            if( cmb.getItemCount() == 0 )   // The DBMS do not support Schemas 
               ded = new DbEntityDescriptor( this.ded.getCatalog(), null, txt.getText().trim() );
            else if( cmb.getSelectedIndex() > -1 )
               ded = new DbEntityDescriptor( this.ded.getCatalog(), (String) cmb.getSelectedItem(), txt.getText().trim() );
            else
               App.showMessage( "Must select a "+ meta.getSchemaTerm() + ". Please try again.");
         }
         else
         {
            App.showMessage( "Diagram name can not be empty. Please try again." );
         }
      }
      
      return ded;
   }

   private void setInvalidActions()
   {
      if( getAllowsChildren() )
      {
         setOpenable(   false );
         setEditable(   false );
         setDeleteable( false );
      }
      else
      {
         setRefreshable( false );
      }
      
      setInfoable( false );
   }
}